package com.hzit.pay.center.service.strategy;

import com.hzit.pay.center.constant.RedisKeyConstant;
import com.hzit.pay.center.mapper.NoticeInfoMapper;
import com.hzit.pay.center.mapper.NoticeRulesInfoMapper;
import com.hzit.pay.center.model.NoticeRulesInfo;
import com.hzit.pay.center.service.INoticeRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class NoticeRuleServiceImpl implements INoticeRuleService {

    @Autowired
    private NoticeRulesInfoMapper noticeRulesInfoMapper;


    @Autowired
    private ValueOperations<String,Object> valueOperations;


    @Override
    public Long getTtl(String count) {


        String redisKey = RedisKeyConstant.NOTICE_RULES_LIST + "1000";

        List<NoticeRulesInfo>  rulesInfoList =  (List<NoticeRulesInfo>) valueOperations.get(redisKey);

        if (CollectionUtils.isEmpty(rulesInfoList)){
            rulesInfoList = noticeRulesInfoMapper.queryAllList();
            valueOperations.set(redisKey,rulesInfoList,2, TimeUnit.HOURS);
        }

        for (NoticeRulesInfo rulesInfo : rulesInfoList){

            if (count.equals(rulesInfo.getNoticeCount())){
                return rulesInfo.getNoticeTimes();
            }
        }

        return null;
    }
}
